// 统一对localStorage做json存取操作

export const setItem = function (key, dataObj) {
    try {
        if (typeof dataObj === 'object') {
            dataObj = JSON.stringify(dataObj)
        }
    } catch (error) {

    }
    window.localStorage.setItem(key, dataObj)
}

export const getItem = function (key) {
    // 三种可能的值：null，json字符串，普通字符串
    let dataStr = window.localStorage.getItem(key)
    try {
        return JSON.parse(dataStr)
    } catch (error) {
        // 如果转换失败，则直接返回读取的值
        return dataStr
    }
}

export const removeItem = function (key) {
    window.localStorage.removeItem(key)
}